package com.cqsw.controller;

import com.cqsw.service.MailService;
import com.cqsw.service.impl.MailServiceImpl;
import com.cqsw.utils.ValiDateCodeGenerateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @Classname HelloController
 * @Description TODO 验证码控制器
 * @Date 2022/6/13 12:46
 * @Created by Smith.Liu
 */
@RestController
public class ValidateController {
    private final static Logger log = LoggerFactory.getLogger(ValidateController.class);
    @Autowired
    private MailService mailService;

    private final StringRedisTemplate stringRedisTemplate;

    @Autowired
    public ValidateController(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        System.out.println(" hello ok ");
        return "hello";
    }

    @RequestMapping(value = "/validateCode", method = RequestMethod.GET)
    public void sendValidateCode(@RequestParam String email) {
        log.info("================================验证码控制器已经进入===================================");
        log.info("邮箱"+email);
        String code = ValiDateCodeGenerateUtils.GenerateFor6();
        String content = "<h2 style='background-color: blanchedalmond;color: coral;font-size: 26px;'>当前验证码10分钟失效:" + code +
                "</h2>";
        mailService.sendWithHtml(email, "当前验证码10分钟失效", content);
        stringRedisTemplate.opsForValue().set("validateCode", code, 15L, TimeUnit.MINUTES);
        log.info("===============================验证码" + (code) + "==================================");
    }
}
